package cn.flying.system.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

import cn.flying.base.common.constants.Constants;
import cn.flying.base.common.utils.StringUtils;
import cn.flying.base.common.utils.ThreadLocalUtil;
import cn.flying.base.core.interceptor.AbstractRequestInterceptor;

/**
 * @description: 操作访问日志记录
 * @author: lvyong
 * @date: 2023年06月05日 19:47
 * @version: 1.0
 */
@Component
public class RequestOperLogInterceptor extends AbstractRequestInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String tenantId = request.getHeader(Constants.TENANT_ID);
        if (StringUtils.isNotBlank(tenantId)) {
            ThreadLocalUtil.set(Constants.TENANT_ID, tenantId);
        } else {

        }
        return true;
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ThreadLocalUtil.remove(Constants.TENANT_ID);
    }

    /**
     * 设置拦截器的执行顺序，返回值越小优先级越高
     *
     * @return
     */
    @Override
    public Integer getOrder() {
        return 2;
    }
}
